package com.mikgonz.farkle.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mikgonz.farkle.R;
import com.mikgonz.farkle.database.Message;

public class MyAdapter extends BaseAdapter {

	private ArrayList<Message> array;
	private LayoutInflater mInflater;

	public MyAdapter(Context c, ArrayList<Message> array) {
		super();
		this.array = array;
		mInflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public Object getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.chat_list_item, parent,
					false);
			holder = new ViewHolder();
			holder.from = (TextView) convertView
					.findViewById(R.id.from_text_view);
			holder.body = (TextView) convertView
					.findViewById(R.id.body_text_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.from.setText(((Message) getItem(position)).getSender());
		holder.body.setText(((Message) getItem(position)).getBody());

		return convertView;
	}

	public void notifyDataSetChanged(ArrayList<Message> array) {
		this.array = array;
		notifyDataSetChanged();
	}

	static class ViewHolder {
		TextView from, body;
	}

	public void addItem(Message message) {
		array.add(message);
		notifyDataSetChanged();
	}

}
